package com.yungam.imcs.controller.dimission_chain;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yungam.common.bean.dimension.MaterialType;
import com.yungam.common.response.RetDTO;
import com.yungam.common.service.MaterialTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Api(tags = "尺寸链材料管理接口")
@RestController
@RequestMapping("/material")
public class MaterialTypeController {

    @Resource
    private MaterialTypeService service;

    @ApiOperation("获取所有的材料类型")
    @GetMapping("/type")
    public RetDTO<?> getMaterialTypes() {
        QueryWrapper<MaterialType> qw = new QueryWrapper<>();
        qw.eq("trash", false);
        return RetDTO.data(service.list(qw));
    }

    @ApiOperation("添加材料")
    @PostMapping("/type")
    public RetDTO<?> addMaterialType(@ApiParam("材料类型实体") @RequestBody MaterialType materialType) {
        // 查重
        QueryWrapper<MaterialType> qw = new QueryWrapper<>();
        qw.eq("name", materialType.getName()).eq("trash", false);
        if (service.list(qw).size() != 0) return RetDTO.error("材料类型名称重复");
        materialType.setCreateTime(new Date());
        materialType.setTrash(false);
        service.save(materialType);
        return RetDTO.success();
    }

    @ApiOperation("修改某一材料")
    @PutMapping("/type")
    public RetDTO<?> updateMaterialType(@ApiParam("材料类型实体") @RequestBody MaterialType materialType) {
        // 查重
        QueryWrapper<MaterialType> qw = new QueryWrapper<>();
        qw.eq("name", materialType.getName());
        if (service.list(qw).size() != 0) return RetDTO.error("材料类型名称重复");
        service.updateById(materialType);
        return RetDTO.success();
    }

    @ApiOperation("删除某一材料类型")
    @DeleteMapping("/type")
    public RetDTO<?> delOneMaterialType(@ApiParam("id") Integer id) {
        MaterialType type = new MaterialType();
        type.setId(id);
        type.setTrash(true);
        service.updateById(type);
        return RetDTO.success();
    }
}
